#coding:utf-8
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django_user_agents.utils import get_user_agent
from django.http.response import HttpResponseRedirect, HttpResponsePermanentRedirect
from lovewith.views import site_info
from lovewith.website.views.modules.category import CategoryApi
from lovewith.share.views.modules.share import ShareApi
from lovewith.website.views.modules.color import ColorApi


#获取目标分类下的灵感数据
def get_share_by_cate(request, cate_id, page_no=1):
    #判断UA
    user_agent = get_user_agent(request)

    cate_data = CategoryApi.check_data(cate_id)
    if not cate_data:
        return HttpResponseRedirect('/')

    page_no = int(page_no)

    if user_agent.is_mobile:
        return render_to_response('mobile/list.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'search_title': cate_data.name,
            'search_link': '/share/ajax/g/share/?cate_id=%s&page=1' % cate_data.id,
            'search_type': 'share',
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        share_data = ShareApi.get_by_cate(cate_id, 1)

        return render_to_response('share/cate-list.jinja', {
            'share_data': share_data.get('share'),
            'cates': CategoryApi.get_all(),
            'cate_data': cate_data,
            'current_page': page_no,  # 显示页码
            'data_page': share_data.get('data_page'),  # 数据页码
            'total_page': share_data.get('total_page')
        }, context_instance=RequestContext(request, processors=[site_info]))


#按颜色搜索
def get_share_by_color(request, color):
    #判断UA
    color_map = {
        u'黑色婚礼': '000000',
        u'白色婚礼': 'fefefe',
        u'灰色婚礼': 'CFCFCF',
        u'裸色婚礼': 'f6d5b2',
        u'金色婚礼': 'ffd700',
        u'褐色婚礼': 'b06f18',
        u'深紫色婚礼': 'C71585',
        u'浅紫色婚礼': 'EEAEEE',
        u'宝蓝色婚礼': '0304e5',
        u'天蓝婚礼': '3CB3EB',
        u'Tiffany蓝婚礼': '66cccc',
        u'墨绿婚礼': '008B00',
        u'薄荷绿婚礼': 'B4EEB4',
        u'黄色婚礼': 'f7de6a',
        u'橙色婚礼': 'ffaf61',
        u'粉红婚礼': 'fdb0b0',
        u'玫红婚礼': 'f15d94',
        u'红色婚礼': 'f26060'
    }

    if color in color_map.keys():
        return HttpResponsePermanentRedirect('/share/search/color/%s/' % color_map.get(color))

    user_agent = get_user_agent(request)
    page_no = int(request.GET.get('page', 1))
    post_data = ShareApi.get_by_color(color, page_no)
    color_data = ColorApi.check_data(color_value=color)

    if user_agent.is_mobile:
        return render_to_response('mobile/list.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'search_title': color_data.name,
            'search_link': '/share/ajax/g/share/?color=%s&page=1' % color,
            'search_type': 'image',
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        return render_to_response('share/search.jinja', {
            'search_type': 'color',
            'search_key': color,
            'color_data': color_data,
            'total': post_data.get('total'),
            'share_data': post_data.get('share')
        }, context_instance=RequestContext(request, processors=[site_info]))


#按tag搜索
def get_share_by_tag(request, tag):
    #判断UA
    user_agent = get_user_agent(request)
    page_no = int(request.GET.get('page', 1))
    post_data = ShareApi().get_by_tag(tag, page_no)

    if user_agent.is_mobile:
        return render_to_response('mobile/list.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'search_title': tag,
            'search_link': '/share/ajax/g/share/?tag=%s&page=1' % tag,
            'search_type': 'image',
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        return render_to_response('share/search.jinja', {
            'search_type': 'tag',
            'search_key': tag,
            'total': post_data.get('total'),
            'share_data': post_data.get('share')
        }, context_instance=RequestContext(request, processors=[site_info]))


#根据关键词搜索标题
def get_share_by_keyword(request, keyword):
    #判断UA
    user_agent = get_user_agent(request)
    page_no = int(request.GET.get('page', 1))
    post_data = ShareApi.get_by_title(keyword, page_no)

    if user_agent.is_mobile:
        return render_to_response('mobile/list.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'search_title': keyword,
            'search_link': '/share/ajax/g/share/?keyword=%s&page=1' % keyword,
            'search_type': 'image',
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        return render_to_response('share/search.jinja', {
            'search_type': 'keyword',
            'search_key': keyword,
            'total': post_data.get('total'),
            'share_data': post_data.get('share')
        }, context_instance=RequestContext(request, processors=[site_info]))


#全部商家的帖子
def get_all_business_share(request):
    post_data = ShareApi.get_by_business()

    return render_to_response('share/business.jinja', {
        'search_type': 'keyword',
        'share_data': post_data
    }, context_instance=RequestContext(request, processors=[site_info]))